package com.lins.storedisk.utils;

import com.lins.disk.common.APIException;
import com.lins.disk.common.AppCode;
import com.lins.disk.entity.UserEntity;
import org.springframework.util.DigestUtils;

import java.lang.reflect.Field;
import java.util.concurrent.atomic.AtomicInteger;

public class LinsUtil {
    /*保证原子性*/
    private static volatile AtomicInteger atomic = new AtomicInteger(0);

    /**
     * 打印(带序号)且返回该值
     **/
    public static String printNo(String s,Object... o){
        return print("N{}: {}",atomic.incrementAndGet(),merge(s,o));
    }

    /**
     * 打印且带返回值
     **/
    public static String print(String s,Object... o){
        String r = merge(s, o);
        System.out.println(r);
        return r;
    }

    /**
     * 返回合并后的字符串
     **/
    public static String merge(String s,Object... o){
        return dg(s, o, 0, "");
    }

    /**
     * @Param: s 需要打印的原始字符串
     * @param o object
     * @param i Ok的计数
     * @param e end：每次截取后的尾部字符串
     **/
    private static String dg(String s, Object[] o, int i, String e){
        if (s.contains("{") && s.contains("}")){
            int l = s.indexOf("{"),r = s.indexOf("}");
            if (l+1 == r){ i++;
                if (r+1 != s.length()) e = dg(s.substring(r+1), o, i,"");
                return !"".equals(e)?s.substring(0,l)+o[i-1]+e:o.length>=i?s.substring(0,l)+o[i-1]+s.substring(r+1):s;
            }else return s;
        }else return s;
    }

    /**
     * 判断是否所有的属性值都是null
     **/
    public static boolean isNullAllField(Object o) {
        try {
            //遍历所有属性
            for (Field field : o.getClass().getDeclaredFields()) {
                //把私有属性公有化
                field.setAccessible(true);
                Object object = field.get(o);
                //若有一个属性不是空，则不符合预期
                if (object!=null) return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 参数是否属于基本数据类型
     **/
    public static <T> boolean isBasicDataType(T o) {
        if (null == o) {
            try {
                throw new IllegalArgumentException("The parameter cannot be null");
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return ( o instanceof Byte
                || o instanceof Short
                || o instanceof Integer
                || o instanceof Long
                || o instanceof Float
                || o instanceof Double
                || o instanceof Boolean
                || o instanceof Character
        );
    }

    /**
     * 清空对象属性
     **/
    public static <T> boolean cleanObjectFields(T o){
        if (isBasicDataType(o) || o instanceof String) {
            try {
                throw new IllegalArgumentException("Parameter is not a reference data type");
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        for (Field field : o.getClass().getDeclaredFields()) {
            try {
                field.setAccessible(true);
                field.set(o,null);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    /**
     * md5加密算法  转字节转大写
     *  加密规则：
     *  1、无视原始密码的强度
     *  2、使用UUID作为盐值，在原始密码的左右两侧拼接
     *  3、循环加密3次
     */
    public static String getMd5Password(String password, String salt) {
        for (int i = 0; i < 3; i++) password = DigestUtils.md5DigestAsHex((salt + password + salt).getBytes()).toUpperCase();
        //加密3次后的密码
        return password;
    }

    public static void iUserNotFoundException(UserEntity result){
        if (result == null) throw new APIException(AppCode.USERNAME_ERROR,"用户不存在");
    }
}